// departmentModel.js
const pool = require("@/db"); // 引入连接池
const table = require("@/db/table"); // 引入连接池

// 根据ID查询角色
async function getDeptById({ id }) {
  return await pool
    .select("*")
    .from(table.DEPARTMENT)
    .where("id", id, "eq", "ifHave")
    .queryRow();
}

// 添加角色
async function addDept(role) {
  const result = await pool
    .insert(table.DEPARTMENT, {
      name: role.name,
      description: role.description,
      created_at: new Date(),
      updated_at: new Date(),
    })
    .execute();
  return result.insertId;
}

// 删除角色
async function deleteDept(id) {
  const result = await pool.delete(table.DEPARTMENT).where("id", id).execute();
  return result.affectedRows > 0;
}

// 更新角色
async function updateDept(id, role) {
  const result = await pool
    .update(table.DEPARTMENT, {
      name: role.name,
      description: role.description,
    })
    .where("id", id)
    .execute();
  return result.affectedRows > 0;
}

// 查询角色列表
async function getDepts({ page_index = 1, page_size = 20 }) {
  return await pool
    .select("*")
    .from(table.DEPARTMENT)
    .queryListWithPaging(page_index, page_size);
}
module.exports = { getDeptById, addDept, deleteDept, updateDept, getDepts };
